/*
 * 文件名: CommonMenuUtil.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XIAOFANQING001
 * 创建时间: 2012-1-16
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package com.longevityassistant.menu;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import app.log.APPLog;
import app.utils.CommonUtils;

import com.longevityassistant.activity.AboutActivity;
import com.longevityassistant.activity.BaseActivity;
import com.longevityassistant.activity.CheckUpdateActivity;
import com.longevityassistant.activity.R;

/**
 * 公共的Menu
 * @author EX-XIAOFANQING001
 * @date 2012-1-16
 * @version [Android PABank C01, @2012-1-16]
 * @description
 */
public class CommonMenuUtil
{
    /**
     * 默认菜单项
     * @author EX-XIAOFANQING001
     * @version [Android PABank C01, 2012-1-16]
     */
    public static enum DefaultMenuType {
        HOME, 
        GUIDE,
        CHECK_UPDATE,
        COMMENT,
        ABOUT,
        EXIT
    }
    
    /**
     * 添加默认的菜单项
     * @param context 上下文对象
     */
    public static List<MenuItem> getDefaultMenus(final Context context) {
        MenuItem menuHome = createSingleDefaultMenu(context, DefaultMenuType.HOME);
        MenuItem menuGuide = createSingleDefaultMenu(context, DefaultMenuType.GUIDE);
        MenuItem menuCheckUpdate = createSingleDefaultMenu(context, DefaultMenuType.CHECK_UPDATE);
        MenuItem menuComment = createSingleDefaultMenu(context, DefaultMenuType.COMMENT);
        MenuItem menuAbout = createSingleDefaultMenu(context, DefaultMenuType.ABOUT);
        MenuItem menuExit = createSingleDefaultMenu(context, DefaultMenuType.EXIT);
        
        List<MenuItem> menus = new ArrayList<MenuItem>();
        
        menus.add(menuHome);
        menus.add(menuGuide);
        menus.add(menuCheckUpdate);
        menus.add(menuComment);
        menus.add(menuAbout);
        menus.add(menuExit);
        
        return menus;
    }
    
    /**
     * 根据默认菜单项的枚举列表返回默认菜单项列表对象
     * @param context 上下文对象
     * @param menus 默认菜单的枚举对象
     * @return 菜单对象列表
     */
    public static List<MenuItem> getDefaultMenus(final Context context, DefaultMenuType ... menus) {
        List<MenuItem> menuItems = new ArrayList<MenuItem>();
        for (int i =0; i<menus.length; i++) {
            DefaultMenuType dmt = menus[i];
            MenuItem mi = createSingleDefaultMenu(context, dmt);
            menuItems.add(mi);
        }
        return menuItems;
    }
    
    
    /**
     * 创建默认的菜单项目
     * @param context
     * @param menuType
     * @return
     */
    public static MenuItem createSingleDefaultMenu(final Context context, DefaultMenuType menuType) {
        MenuItem menuItem = null;
        switch (menuType) {
            case HOME:
                String homeTitle = context.getString(R.string.menu_home);
                menuItem = new MenuItem(homeTitle, R.drawable.icon_menu_home, new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        addWebTrends(context, v);
//                        Intent i = new Intent(context, PingAnHomeActivity.class);
//                        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                        context.startActivity(i);
                    }
                });
                break;
            case GUIDE:
                menuItem = new MenuItem(context.getString(R.string.menu_guide), R.drawable.icon_menu_guide, new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                         addWebTrends(context, v);
//                    	 Intent i = new Intent(context, HelperListActivity.class);
//                         context.startActivity(i);
                    }
                });
                break;
            case CHECK_UPDATE:
                menuItem = new MenuItem(context.getString(R.string.menu_check_update), R.drawable.icon_menu_update, new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        addWebTrends(context, v);
                    	Intent intent = new Intent(context, CheckUpdateActivity.class);
                    	context.startActivity(intent);
                    }
                });
                break;
            case COMMENT:
                menuItem = new MenuItem(context.getString(R.string.menu_comment), R.drawable.icon_menu_comment, new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        
//                        Intent i = new Intent(context, OpinionFeedbackActivity.class);
//                        context.startActivity(i);
                    }
                });
                break;
            case ABOUT:
                menuItem = new MenuItem(context.getString(R.string.menu_about), R.drawable.icon_menu_about, new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        
                        Intent i = new Intent(context, AboutActivity.class);
                        context.startActivity(i);
                    }
                });
                break;
            case EXIT:
                menuItem = new MenuItem(context.getString(R.string.menu_exit), R.drawable.icon_menu_exit, new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        addWebTrends(context, v);
                        
//                        try {
//                            PingAnHomeActivity pingAnHomeActivity = (PingAnHomeActivity) context;
//                            CommonUtils.showTipDialog(context, context.getString(R.string.msg_exit), null);
//                        } catch (Exception e) {
//                            CommonUtils.showTipDialog(context, context.getString(R.string.msg_exit));    
//                        }
                    }
                });
                break;
        }
        return menuItem;
    }
    
    /**
     * 为按钮添加WebTrends收集
     * @param context
     * @param v
     */
    private static void addWebTrends(final Context context, final View v) {
        BaseActivity baseActivity  = null;
        String pageTitle = "登陆界面";
        try {
            baseActivity = (BaseActivity) context;
            pageTitle = baseActivity.getTopTitle();
        } catch (Exception e) {
            // ignore
            APPLog.e("CommonMenuUtil",  "menu:" + e.getMessage());
        }
        APPLog.e("CommonMenuUtil",  "pageTitle:" + pageTitle);
        
        TextView tvMenuTitile = (TextView)v.findViewById(R.id.tv_title);
        String title = tvMenuTitile.getText().toString();
        
    }
}

